﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;
using YasharEl.Infrastructure.ProcessEngine.ServiceModel;
using YasharEl.Infrastructure.ServiceModel;

namespace YasharEl.Infrastructure.AllModules.AcceptanceTests.ProcessEngine.Steps
{
    [Binding]
    public class RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFaultStepDefinition
    {
        #region Fields

        private RetrieveProcessRequest _retrieveProcessRequest;
        private ProcessNotFoundFault _thrownFault;

        #endregion

        #region Given

        [Given(@"I have filled out the not existing process workfow id : '(.*)'")]
        [Scope(Tag = "RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFault")]
        public void GivenIHaveFilledOutTheNotExistingProcessWorkfowId(string workflowInstanceId)
        {
            _retrieveProcessRequest = new RetrieveProcessRequest()
            {
                WorkflowInstanceId = new Guid(workflowInstanceId)
            };
        }

        [Given(@"I have filled out the application id : (.*)")]
        [Scope(Tag = "RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFault")]
        public void GivenIHaveFilledOutTheApplicationId(long applicationId)
        {
            _retrieveProcessRequest.ApplicationId = applicationId;
        }

        #endregion

        #region When

        [When(@"I try to retrieve not existing process from processes query service")]
        [Scope(Tag = "RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFault")]
        public void WhenITryToRetrieveNotExistingProcessFromProcessesQueryService()
        {
            try
            {
                using (ServiceProxy<IProcessRetriever> processRetrieverProxy = new ServiceProxy<IProcessRetriever>())
                {
                    processRetrieverProxy.Proxy.RetrieveProcess(_retrieveProcessRequest);
                }
            }
            catch (FaultException<ProcessNotFoundFault> e)
            {
                _thrownFault = e.Detail;
            }
        }

        #endregion

        #region Then

        [Then(@"process not found fault thrown")]
        [Scope(Tag = "RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFault")]
        public void ThenProcessNotFoundFaultThrown()
        {
            Assert.IsNotNull(_thrownFault);
        }

        [Then(@"fault message contains the given not existing process workflow id: '(.*)'")]
        [Scope(Tag = "RetrievingNotExistingProcessInstanceShouldThrownProcessNotFoundFault")]
        public void ThenFaultMessageContainsTheGivenNotExistingProcessWorkflowId(string workflowInstanceId)
        {
            Assert.AreEqual<Guid>(new Guid(workflowInstanceId), _thrownFault.WorkflowInstanceId);
        }


        #endregion
    }
}
